package com.cangxuan.manage.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

/**
 * 文件相关接口
 */
@Slf4j
@Api(tags = "文件相关接口")
@RestController
@RequestMapping("file")
public class FileController extends BaseController {

    @Value("${webPath}")
    String webPath;

    /**
     * 文件上传
     * @param partFile
     * @return
     */
    @ApiOperation("上传图片")
    @PostMapping("uploadImage")
    public Object uploadImage(@ModelAttribute MultipartFile partFile) throws IOException {
        //文件web路径
        String filePath = "/upload/goods/" + System.currentTimeMillis() + ".png";
        //设置文件保存的磁盘路径
        String uploadPath = webPath + filePath;
        //实例一下文件
        File imageFile = new File(uploadPath);
        //将接收到的上传对象写入到imageFile对象中
        partFile.transferTo(imageFile);
        return success("上传成功", filePath);
    }

}
